﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace BDShop.Framework.Utility
{
    public class ObjectUtility
    {
        public static List<Tuple<string, string, string>> GetDifferences<TEntity>(TEntity oldEntity, TEntity newEntity)
        {
            var differences = new List<Tuple<string, string, string>>();

            foreach (PropertyInfo property in oldEntity.GetType().GetProperties())
            {
                object oldValue = property.GetValue(oldEntity, null);
                object newValue = property.GetValue(newEntity, null);


                if (property.PropertyType.IsValueType || property.PropertyType == typeof(string))
                {
                    if (oldValue == null)
                    {
                        if (newValue != null)
                        {
                            differences.Add(new Tuple<string, string, string>(property.Name, "null", newValue.ToString()));
                        }
                    }
                    else
                    {
                        if (newValue == null)
                        {
                            differences.Add(new Tuple<string, string, string>(property.Name, oldValue.ToString(), "null"));
                        }
                        else if (oldValue.Equals(newValue) == false)
                        {
                            differences.Add(new Tuple<string, string, string>(property.Name, oldValue.ToString(), newValue.ToString()));
                        }
                    }
                }



            }

            return differences;
        }
    }
}
